package student.list.updateStudent;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.MysqlMethod;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/student5")
public class SelectStudentDetails extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");//转码
        SelectStudentList selectStudentList = new SelectStudentList();
        selectStudentList.setCode(0);//设置code值,0代表请求成功
        selectStudentList.setMsg("success");//设置MSg的值
        //获取前端传过来的studentId
        String  studentId1 = req.getParameter("studentId");
        int studentId = Integer.parseInt(studentId1);
        String[] a = MysqlMethod.selectSingle(studentId);//查询数据库,获取数据
        //数据类型转换
         Integer a1 = Integer.valueOf(a[0]);
         Integer a2 = Integer.valueOf(a[1]);
         Integer a3 = Integer.valueOf(a[8]);
         //给selectStudentInfo对象赋初值
         SelectStudentInfo selectStudentInfo = new SelectStudentInfo(a1,a2,a[2],a[3],a[4],a[5],a[6],a[7],a3,a[9]);
         //把selectStudentInfo的值加入selectStudentList(selectStudentInfo是selectStudentList的一个属性,所以也要把selectStudentInfo的值赋给selectStudentList)
         selectStudentList.setData(selectStudentInfo);
         //将selectStudentList转为json格式
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(selectStudentList);
        //向前端传值
        resp.getWriter().write(result);

    }


}
